//
//  main.cpp
//  cpp_call_lua
//
//  Created by Ge Jian Feng on 14-5-18.
//  Copyright (c) 2014年 Ge Jian Feng. All rights reserved.
//

#include <iostream>

#ifdef __cplusplus
extern "C" {
#endif
    
#include "Lua/lua.h"
#include "Lua/lauxlib.h"
#include "Lua/luaconf.h"
#include "Lua/lualib.h"
    
#ifdef __cplusplus
}
#endif

int luaAdd(lua_State *L, int x, int y);

int main(int argc, const char * argv[])
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    
    luaL_dofile(L, "main.lua");
    
    int sum = luaAdd(L, 10, 2);
    
    std::cout << "Sum: " << sum << std::endl;
    
    lua_close(L);
    
    return 0;
}

int luaAdd(lua_State *L, int x, int y)
{
    int sum = 0;
    
    lua_getglobal(L, "add");
    lua_pushnumber(L, x);
    lua_pushnumber(L, y);
    lua_call(L, 2, 1);
    sum = lua_tonumber(L, -1);
    lua_pop(L, 1);
    
    return sum;
}